package com.hwl.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hwl.eduservice.feign.OssFileService;
import com.hwl.eduservice.mapper.CourseMapper;
import com.hwl.eduservice.mapper.TeacherMapper;
import com.hwl.eduservice.pojo.Course;
import com.hwl.eduservice.pojo.Teacher;
import com.hwl.eduservice.pojo.vo.TeacherQueryVo;
import com.hwl.eduservice.service.TeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author long
 * @since 2021-09-21
 */
@Slf4j
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Resource
    private OssFileService ossFileService;
    @Resource
    private CourseMapper courseMapper;

    @Override
    public IPage<Teacher> selectPage(Page<Teacher> teacherPage, TeacherQueryVo teacherQueryVo) {
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("sort");

        if (StringUtils.isEmpty(teacherQueryVo)) {
            return baseMapper.selectPage(teacherPage, wrapper);
        }

        String name = teacherQueryVo.getName();
        Integer level = teacherQueryVo.getLevel();
        String joinDateBegin = teacherQueryVo.getJoinDateBegin();
        String joinDateEnd = teacherQueryVo.getJoinDateEnd();

        if (!StringUtils.isEmpty(name)) wrapper.like("name", name);
        if (!StringUtils.isEmpty(level)) wrapper.eq("level", level);
        if (!StringUtils.isEmpty(joinDateBegin)) wrapper.ge("join_date", joinDateBegin);
        if (!StringUtils.isEmpty(joinDateEnd)) wrapper.le("join_date", joinDateEnd);

        return baseMapper.selectPage(teacherPage, wrapper);
    }

    @Override
    public Teacher getTeaByName(String name) {
        return baseMapper.getTeaByName(name);
    }

    @Override
    public List<Map<String, Object>> selectNameList(String keyword) {
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
        wrapper.select("name").like("name", keyword).orderByAsc("sort");
        return baseMapper.selectMaps(wrapper);
    }

    @Override
    public boolean removeAvatarById(String id) {
        Teacher teacher = baseMapper.selectById(id);
        if (teacher != null && !StringUtils.isEmpty(teacher.getAvatar())) {
            Boolean flag = ossFileService.removeFile(teacher.getAvatar()).getSuccess();
            log.info(flag ? "远程调用删除讲师头像成功！" : "远程调用删除讲师头像失败！");
            return flag;
        }
        return true;
    }

    @Override
    public Map<String, Object> selectTeacherInfo(String id) {
        Teacher teacher = baseMapper.selectById(id);
        List<Course> courses = courseMapper.selectList(new QueryWrapper<Course>().eq("teacher_id", id));

        Map<String, Object> map = new HashMap<>();
        map.put("teacher", teacher);
        map.put("courseList", courses);
        return map;
    }

    @Cacheable(value = "index", key = "'selectHot'")
    @Override
    public Map<String, Object> selectHot() {
        QueryWrapper<Course> courseQueryWrapper = new QueryWrapper<Course>().orderByDesc("view_count").last("limit 8");
        List<Course> courses = courseMapper.selectList(courseQueryWrapper);

        QueryWrapper<Teacher> teacherQueryWrapper = new QueryWrapper<Teacher>().orderByAsc("sort").last("limit 4");
        List<Teacher> teachers = baseMapper.selectList(teacherQueryWrapper);

        Map<String, Object> map = new HashMap<>();
        map.put("teacherList", teachers);
        map.put("courseList", courses);
        return map;
    }
}
